package com.xbongbong.parent.explain;

import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.parent.abstracts.AbstractFormExplainHandleName;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.formexplain.pojo.dto.HandlerExplainDTO;
import com.xbongbong.pro.formexplain.pojo.vo.HandlerExplainVO;
import com.xbongbong.saas.enums.business.InstockEnum;
import com.xbongbong.saas.util.DepartmentFieldUtil;
import com.xbongbong.saas.util.UserFieldUtil;
import com.xbongbong.sys.domain.entity.DepartmentEntity;
import com.xbongbong.sys.domain.entity.UserEntity;
import com.xbongbong.sys.model.DepartmentModel;
import com.xbongbong.sys.model.UserModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * @author: wufeng
 * @date: 2018/11/2 10:29
 * @desrcption: 表单数据详情获取字段回显名称，以及过滤不可见字段
 */
@Component
public class FormDataDetailFormExplainHandleName extends AbstractFormExplainHandleName {

    private static final Logger LOG = LoggerFactory.getLogger(FormDataDetailFormExplainHandleName.class);


    @Resource
    private UserModel userModel;
    @Resource
    private DepartmentModel departmentModel;



    public HandlerExplainVO handleExplain(List<FieldAttrEntity> explainList, PaasFormEntityExt formEntity, PaasFormDataEntityExt paasFormDataEntityExt, HandlerExplainDTO handlerExplainDTO) throws XbbException {
        // ---- 获取用户和部门容器start
        Set<String> userIdList = UserFieldUtil.getUserIdList(explainList, paasFormDataEntityExt);
        userIdList.addAll(handlerExplainDTO.getOwnerId());
        userIdList.addAll(handlerExplainDTO.getCoUserId());
        List<UserEntity> list = userModel.getByUserIdIn(handlerExplainDTO.getCorpid(), userIdList, null);
        Map<String, UserEntity> userMap = new HashMap<>(list.size());
        list.forEach(item->{
            userMap.put(item.getUserId(), item);
        });
        Set<Long> departmentIdList = DepartmentFieldUtil.getDepartmentIdList(explainList, paasFormDataEntityExt);
        List<DepartmentEntity> departmentList = departmentModel.getDepartmentNamesByIds(handlerExplainDTO.getCorpid(), departmentIdList, false);
        Map<String, DepartmentEntity> departmentMap = new HashMap<>(departmentList.size());
        departmentList.forEach(item->{
            departmentMap.put(item.getId().toString(), item);
        });
        handlerExplainDTO.setExplainList(explainList);
        handlerExplainDTO.setPaasFormEntityExt(formEntity);
        // 获取协同人和负责人（产品是可见人可见部门）

        HandlerExplainVO handlerExplainVO = handleExplain(handlerExplainDTO);
        handlerExplainVO.setCoUserId(handlerExplainDTO.getCoUserId());
        handlerExplainVO.setOwnerId(handlerExplainDTO.getOwnerId());
        handlerExplainVO.setUserMap(userMap);
        handlerExplainVO.setDepartmentMap(departmentMap);
        return handlerExplainVO;
    }

    @Override
    public HandlerExplainVO  handleExplain(HandlerExplainDTO handlerExplainDTO) throws XbbException {
        HandlerExplainVO handlerExplainVO;
        try {
            List<FieldAttrEntity> explainList = handlerExplainDTO.getExplainList();

            // TODO 工单的待处理
            Iterator<FieldAttrEntity> iterator = explainList.iterator();
            // 过滤掉关联查询的字段
            while (iterator.hasNext()) {
                // 详情不显示关联查询
                FieldAttrEntity entity = iterator.next();
                if(Objects.equals(entity.getSaasAttr(), InstockEnum.PRODUCT_INFO.getSaasAttr())){
                    iterator.remove();
                } else if (Objects.equals(entity.getFieldType(), FieldTypeEnum.LINK_QUERY.getType())) {
                    iterator.remove();
                } else if (Objects.equals(entity.getFieldType(), FieldTypeEnum.SUB_FORM.getType())) {
                    List<? extends FieldAttrEntity> subExplainList = entity.getSubForm().getItems();
                    Iterator<? extends FieldAttrEntity> subIterator = subExplainList.iterator();
                    while (subIterator.hasNext()) {
                        FieldAttrEntity subEntity = subIterator.next();
                        if (Objects.equals(subEntity.getFieldType(), FieldTypeEnum.LINK_QUERY.getType())) {
                            subIterator.remove();
                        }
                    }
                }
            }
            // 根据表单的权限剔除字段
            handlerExplainVO = getExplainList(explainList, handlerExplainDTO);
            handleExplainDefaultLimit(handlerExplainVO, handlerExplainDTO);
        } catch (XbbException e) {
            throw e;
        } catch (Exception e){
            LOG.error("FormDataFormExplainHandleName.handleExplain处理字段中的部门、角色、用户等名称报错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return handlerExplainVO;
    }

}
